﻿using DLL;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.PICCBloodPurification
{
    public partial class AddPICCBloodPurification : Form
    {
        #region 变量声明
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        NursingFormRecord record = new NursingFormRecord();
        public NursingFormSystem.WebReference2.FunctionAndPermissions WebScale = new NursingFormSystem.WebReference2.FunctionAndPermissions();
        PatientInfo patientinfo;
        string Id = string.Empty;
        Tb_User user;
        string IsNewPage = string.Empty;  //判断是否新增还是新起一次

        DateTime date = DateTime.Now; //当前时间
        string sdate = DateTime.Now.ToString("yyyy-MM-dd"); //当时间建
        #endregion

        #region  窗体构造函数
        //外部调用
        public AddPICCBloodPurification(string[] loginUser, string[] patientInfo)
        {
            InitializeComponent();
            this.patientinfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            this.user = ArrayToObject.ArrayToLoginUser(loginUser);
            this.Id = string.Empty;
            this.IsNewPage = "N";
            BandInfo(Id);
        }
        public AddPICCBloodPurification(string[] loginUser, string[] patientInfo, string Id)
        {
            InitializeComponent();
            this.patientinfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            this.user = ArrayToObject.ArrayToLoginUser(loginUser);
            this.Id = Id;
            this.IsNewPage = "N";
            BandInfo(Id);
        }
        //新增
        public AddPICCBloodPurification(Tb_User loginUser, PatientInfo patientInfo, string Id, string isNewPage)
        {
            InitializeComponent();
            this.patientinfo = patientInfo;
            this.user = loginUser;
            this.Id = Id;
            this.IsNewPage = isNewPage;
            BandInfo(Id);

        }
        //新起一页点击进来  构造函数
        public AddPICCBloodPurification(Tb_User loginUser, PatientInfo patientInfo, string Id, string isNewPage, string num)
        {
            InitializeComponent();
            this.patientinfo = patientInfo;
            this.user = loginUser;
            this.Id = Id;
            this.IsNewPage = isNewPage;
            if (isNewPage.Equals("Y"))
            {
                txtNewNum.Text = (int.Parse(num) + 1).ToString();
            }
            else
            {
                txtNewNum.Text = num;
            }
            BandInfo(Id);
        }
        #endregion

        #region 保存
        private void simplebaocun_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(dateriqi.Text))
                dateriqi.DateTime = DateTime.Now;
            BloodPurification ldf = new BloodPurification();
            ldf.ZhiLiaoMoShi = comboZhiliaomoshi.Text;   //治疗模式
            ldf.XueGuanTongLu = comboXueguantonglu.Text;   //血管通路
            ldf.GuoLvHingHao = comboGuolvxinghao.Text;   //过滤型号
            ldf.KangNingFangShi = comboKangningfangshi.Text;   //抗凝方式
            ldf.RiQi = dateriqi.Text;    //日期
            ldf.ShiJian = txtShijian.Text; //时间
            ldf.ShengMingTiZhengT = textshengmingtizhengT.Text;   //生命体征--T
            ldf.ShengMingTiZhengHR = textshengmingtizhengHR.Text;  //生命体征--HR
            ldf.ShengMingTiZhengR = textshengmingtizhengR.Text;     //生命体征--R
            ldf.ShengMingTiZhengBP = textshengmingtizhengBP.Text;   //生命体征--BP
            ldf.YaLiJianCeShuRuYa = textshuruya.Text;      //压力检测--输入压
            ldf.YaLiJianCeHuiShuYa = texthuishuya.Text;    //压力检测--回输压
            ldf.YaLiJianCeTMP = textTMP.Text;             //压力检测--TMP
            ldf.ChuFangXueLiuLiang = textxueliuliang.Text;         //处方--血流量
            ldf.ChuFangPBP = textPBP.Text;           //处方--PBP
            ldf.ChuFangTouXiYe = texttouxiye.Text;           //处方--透析液
            ldf.ChuFangZhiHuanYeQian = textzhihuanyeqian.Text;      //处方--置换液-前
            ldf.ChuFangZhiHuanYeHou = textzhihuanyehou.Text;       //处方--置换液-后
            ldf.ChuFangTuoShuiLiang = texttuoshuiliang.Text;      //处方--脱水量
            ldf.ChuFangKangNingJi = textkangningji.Text;         //处方--抗凝剂
            ldf.BingQingGuanChaJiChuLi = memobingqingguanchajichuli.Text;    //病情观察及处理
            //护士
            if (string.IsNullOrEmpty(Id))  //新增
            {
                if (date > Convert.ToDateTime(sdate + " 08:00:00") && date < Convert.ToDateTime(sdate + " 17:59:59"))
                {
                    ldf.HuShi = user.UserName;     //白班护士
                    ldf.ShangYeHuShi = string.Empty;
                    ldf.XiaYeHuShi = string.Empty;
                }
                else if (date > Convert.ToDateTime(sdate + " 18:00:00") && date < Convert.ToDateTime(sdate + " 23:59:59"))
                {
                    ldf.ShangYeHuShi = user.UserName;  //上夜护士
                    ldf.HuShi = string.Empty;
                    ldf.XiaYeHuShi = string.Empty;
                }
                else
                {
                    ldf.XiaYeHuShi = user.UserName;  //下夜护士  00:00-07:59:59
                    ldf.HuShi = string.Empty;
                    ldf.ShangYeHuShi = string.Empty;
                }
            }
            //修改
            else
            {
                ldf.HuShi = texthushi.Text;     //白班护士               
                ldf.ShangYeHuShi = txtShangyehushi.Text;  //上夜护士               
                ldf.XiaYeHuShi = txtXiayehushi.Text;  //下夜护士  
            }

            if (string.IsNullOrEmpty(txtNewNum.Text))
            {
                txtNewNum.Text = "1";
            }
            ldf.Num = int.Parse(txtNewNum.Text.ToString());  //次数
            ldf.IsNewPageFlag = txtIsNewPage.Text;  //是否新起一页

            ldf.FormId = (int)FormType.BloodPurification;      //表单ID
            ldf.FormName = EnumHelper.GetDescription(FormType.BloodPurification);    //表单名字
            record.FormId = (int)FormType.BloodPurification;  //表单ID
            record.FormName = EnumHelper.GetDescription(FormType.BloodPurification);      //表单名字
            record.PatientName = patientinfo.PATIENT_NAME;     //患者姓名
            record.PatientId = patientinfo.PATIENT_ID;   //住院号
            record.VisitId = Convert.ToInt32(patientinfo.VISIT_ID);   //住院次数
            record.NursingFormData = XmlUtil.ObjectToXML(ldf, Encoding.UTF8);
            record.AssessmentDate = Convert.ToDateTime(ldf.RiQi + " " + ldf.ShiJian);    //日期+时间
            record.BedNo = patientinfo.BED_NO;   //床号
            record.DepartmentId = patientinfo.DEPARTMENT_ID;   //当前科室ID
            record.DepartmentName = patientinfo.DEPARTMENT_NAME;   //当前科室名字
            record.AreaId = patientinfo.NURSE_CELL_CODE;    //当前患者病区ID
            record.AreaName = patientinfo.NURSE_CELL_NAME;    //当前患者病区名称
            record.Id = Id == "" ? 0 : Convert.ToInt32(Id);
            var ret2 = formService.SetNursingFormRecord(Newtonsoft.Json.JsonConvert.SerializeObject(record));
            if (ret2.S == true)
            {
                MessageBox.Show(ret2.M);
            }
            else
            {
                MessageBox.Show(ret2.M);
            }
            this.DialogResult = DialogResult.OK;   //点击保存后，退出页面
        }
        #endregion

        #region 点击修改时，显示相对应的值
        public void BandInfo(string Id)
        {
            dateriqi.DateTime = DateTime.Now;
            #region 自动获取数据
            if (date > Convert.ToDateTime(sdate + " 08:00:00") && date < Convert.ToDateTime(sdate + " 17:59:59"))
            {
                texthushi.Text = user.UserName;     //白班护士
            }
            else if (date > Convert.ToDateTime(sdate + " 18:00:00") && date < Convert.ToDateTime(sdate + " 23:59:59"))
            {
                txtShangyehushi.Text = user.UserName;  //上夜护士
            }
            else
            {
                txtXiayehushi.Text = user.UserName;  //下夜护士  00:00-07:59:59
            }
            txtIsNewPage.Text = this.IsNewPage;
            #endregion
            txtShijian.Text = DateTime.Now.ToString("HH:mm");   //时间
            if (string.IsNullOrEmpty(Id)) return;
            string sql = string.Format("SELECT NursingFormData FROM dbo.NursingFormRecord(NOLOCK) WHERE Id= '{0}' AND FormId = 18005", Id);
            DataTable dtb = SqlHelper.ExecuteDataset(sql, System.Data.CommandType.Text).Tables[0];
            var NursingFormData = Convert.ToString(dtb.Rows[0]["NursingFormData"]);
            BloodPurification ldf = new BloodPurification();
            ldf = XmlUtil.XMLToObject<BloodPurification>(NursingFormData, Encoding.UTF8);
            dateriqi.DateTime = Convert.ToDateTime(ldf.RiQi.ToString());   //日期
            txtShijian.Text = DateTimeHelper.HHmmssToHHmm(ldf.ShiJian);  //时间
            comboZhiliaomoshi.Text = ldf.ZhiLiaoMoShi;     //治疗模式
            comboXueguantonglu.Text = ldf.XueGuanTongLu;     //血管通路
            comboGuolvxinghao.Text = ldf.GuoLvHingHao;     //过滤型号
            comboKangningfangshi.Text = ldf.KangNingFangShi;     //抗凝方式
            textshengmingtizhengT.Text = ldf.ShengMingTiZhengT;    //生命体征--T
            textshengmingtizhengHR.Text = ldf.ShengMingTiZhengHR;   //生命体征--HR
            textshengmingtizhengR.Text = ldf.ShengMingTiZhengR;     //生命体征--R
            textshengmingtizhengBP.Text = ldf.ShengMingTiZhengBP;    //生命体征--BP
            textshuruya.Text = ldf.YaLiJianCeShuRuYa;    //压力检测--输入压
            texthuishuya.Text = ldf.YaLiJianCeHuiShuYa;    //压力检测--回输压
            textTMP.Text = ldf.YaLiJianCeTMP;    //压力检测--TMP
            textxueliuliang.Text = ldf.ChuFangXueLiuLiang;     //处方--血流量
            textPBP.Text = ldf.ChuFangPBP;    //处方--PBP
            texttouxiye.Text = ldf.ChuFangTouXiYe;    //处方--透析液
            textzhihuanyeqian.Text = ldf.ChuFangZhiHuanYeQian;     //处方--置换液--前
            textzhihuanyehou.Text = ldf.ChuFangZhiHuanYeHou;      //处方--置换液--后
            texttuoshuiliang.Text = ldf.ChuFangTuoShuiLiang;     //处方--脱水量
            textkangningji.Text = ldf.ChuFangKangNingJi;      //处方--抗凝剂
            memobingqingguanchajichuli.Text = ldf.BingQingGuanChaJiChuLi;      //病情观察及处理
            texthushi.Text = ldf.HuShi;  //白班护士
            txtShangyehushi.Text = ldf.ShangYeHuShi;//上夜护士
            txtXiayehushi.Text = ldf.XiaYeHuShi;  //下夜护士
            txtNewNum.Text = ldf.Num.ToString();   //次数
            txtIsNewPage.Text = ldf.IsNewPageFlag;  //是否新起一页
        }

        #endregion

        #region 取消
        private void simplequxiao_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
        #endregion

        #region 设置 enter键当做Tab切换
        /// <summary>
        /// 设置 enter键当做Tab切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TemperatureEdit1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }
        #endregion

        #region  时间双击事件
        private void textEdit1_DoubleClick(object sender, EventArgs e)
        {
            txtShijian.Text = DateTime.Now.ToString("HH:mm");
        }
        #endregion
    }
}